package com.easy.work.function.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andview.refreshview.utils.Utils;
import com.base.syslib.pulltorefresh.PullToRefreshBase;
import com.base.syslib.pulltorefresh.PullToRefreshGridView;
import com.base.syslib.util.AndroidUtils;
import com.base.syslib.util.DensityUtil;
import com.base.syslib.util.MessageTools;
import com.base.syslib.util.TextUtil;
import com.base.syslib.view.HeaderGridView;
import com.easy.work.R;
import com.easy.work.app.AppSession;
import com.easy.work.app.BaseActivity;
import com.easy.work.function.JobFilterListAct;
import com.easy.work.function.main.adapter.JobSearchAdapter;
import com.easy.work.http.request.JobFilterRequest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhangfeng
 * @date 2018/01/02
 * @desc
 */

public class JobSearchActivity extends BaseActivity {

    public static void start(Context context) {
        context.startActivity(new Intent(context, JobSearchActivity.class));
    }

    @BindView(R.id.ly_top)
    LinearLayout lyTop;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.refreshGridView)
    GridView refreshGridView;
    private JobFilterRequest request = new JobFilterRequest();
    private JobSearchAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        isSetBatteryBar = true;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_search);
        ButterKnife.bind(this);
        request.city = AppSession.getInstance().getCityCode();
        request.latitude = AppSession.getInstance().getLatitude();
        request.longitude = AppSession.getInstance().getLontitude();
        int statusHeight = Utils.getStatusHeight(this);
        if (statusHeight == -1) {
            statusHeight = DensityUtil.dip2px(this, 25);
        }
        lyTop.setPadding(0, statusHeight, 0, 0);

//        refreshGridView.setPullLoadEnabled(true);
//        refreshGridView.setPullRefreshEnabled(true);
//        refreshGridView.setOnRefreshListener(this);
//        HeaderGridView gridView = refreshGridView.getRefreshableView();
//        gridView.setNumColumns(3);
//        gridView.setVerticalSpacing(DensityUtil.dip2px(this, 17));
//        int hs = (Utils.getScreenWidth(this) - DensityUtil.dip2px(this, 30) - DensityUtil.dip2px(this, 87.5f * 3)) / 3;
//        gridView.setHorizontalSpacing(hs);
//        gridView.setColumnWidth(DensityUtil.dip2px(this, 87.5f));
//        View headView = getLayoutInflater().inflate(R.layout.head_job_search, gridView, false);
//        gridView.addHeaderView(headView);

        List<String> list = new ArrayList<>();
        String[] arr = {"服务员", "普工", "营业员", "快递员", "销售", "保安"};
        list.addAll(Arrays.asList(arr));
        mAdapter = new JobSearchAdapter(this, 1, list);
        refreshGridView.setAdapter(mAdapter);
        refreshGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String searchMsg = mAdapter.getItem(position);
                request.title = searchMsg;
                request.searchType = "0";
                JobFilterListAct.start(JobSearchActivity.this, "职位搜索", request);
            }
        });

//        AppSession.setPullFormartRefreshTime(refreshGridView);
//        refreshGridView.doPullRefreshing(true, 0);

        etContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    AndroidUtils.closeKeyboard(getApplicationContext(), etContent);
                    String searchMsg = etContent.getText().toString().trim();
                    if (TextUtil.isNotNull(searchMsg)) {
                        request.title = searchMsg;
                        request.searchType = "0";
                        JobFilterListAct.start(JobSearchActivity.this, "职位搜索", request);
                    }


                }
                return false;
            }
        });
    }

//    @Override
//    public void onPullDownToRefresh(PullToRefreshBase<HeaderGridView> refreshView) {
//        refreshGridView.onPullUpRefreshComplete();
//        refreshGridView.onPullDownRefreshComplete();
//    }
//
//    @Override
//    public void onPullUpToRefresh(PullToRefreshBase<HeaderGridView> refreshView) {
//        refreshGridView.onPullUpRefreshComplete();
//        refreshGridView.onPullDownRefreshComplete();
//    }

    @OnClick(R.id.tv_cancel)
    void cancel() {
        finish();
    }
}
